<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>面向对象 Tab</title>
    <link rel="stylesheet" href="./styles/tab.css">
    <link rel="stylesheet" href="./styles/style.css">
</head>

<body>

    <main>
        <h4>
            Js 面向对象 动态添加标签页
        </h4>
        <div class="tabsbox" id="tab">
            <!-- tab 标签 -->
            <nav class="fisrstnav">
                <ul>
                    <li class="liactive"><span>测试1</span><span class="iconfont icon-guanbi"></span></li>
                    <li><span>测试2</span><span class="iconfont icon-guanbi"></span></li>
                    <li><span>测试3</span><span class="iconfont icon-guanbi"></span></li>
                </ul>
                <div class="tabadd">
                    <span>+</span>
                </div>
            </nav>

            <!-- tab 内容 -->
            <div class="tabscon">
                <section class="conactive">测试1</section>
                <section>测试2</section>
                <section>测试3</section>
            </div>
        </div>
    </main>

    <script src="js/tab.js"></script>
</body>

<script>
    let that = null;
    class Tab {
        constructor(id) {
            this.main = document.querySelector(id);
            this.add = this.main.querySelector('.tabadd');
            this.ul = this.main.querySelector('.fisrstnav ul');
            this.tabscon = this.main.querySelector('.tabscon');
            this.init();
            that = this;
        }

        updateNode() {
            this.lis = this.main.querySelectorAll('li');
            this.sections = this.main.querySelectorAll('section');
            this.guanbis = this.main.querySelectorAll('.icon-guanbi');
            this.spams = this.main.querySelectorAll('.fisrstnav li span:first-child')
        }

        init() {
            this.updateNode();
            this.add.onclick = this.addTab;
            for (let i = 0; i < this.lis.length; i++) {
                this.lis[i].index = i;
                this.lis[i].onclick = this.toggleTab;
                this.guanbis[i].onclick = this.removeTab;
                this.spams[i].ondblclick = this.editTab;
                this.sections[i].ondblclick = this.editTab;
            }
        }
        toggleTab() {
            that.clearClass();
            this.className = 'liactive';
            that.sections[this.index].className = 'conactive';
        }

        clearClass() {
            for (let i = 0; i < this.lis.length; i++) {
                this.lis[i].className = '';
                this.sections[i].className = '';
            }
        }
        addTab() {
            that.clearClass();
            let li = '<li class="liactive"><span>测试1</span><span class="iconfont icon-guanbi"></span></li>'
            let section = '<section class="conactive">测试1</section>'
            that.ul.insertAdjacentHTML('beforeend', li);
            that.tabscon.insertAdjacentHTML('beforeend', section);
            that.init();
        }
        removeTab(e) {
            e.stopPropagation();
            let index = this.parentNode.index;
            that.lis[index].remove();
            that.sections[index].remove();
            that.init();
            if (document.querySelector('.liactive'))
                return;
            index;
            if (that.lis.length == 0)
                return;
            if (index == that.lis.length)
                index--;
            that.lis[index].click();
        }
        editTab() {
            let str = this.innerHTML;
            window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
            this.innerHTML = '<input type="text"/>'
            let input = this.children[0];
            input.value = str;
            input.select();
            input.onblur = function() {
                this.parentNode.innerHTML = this.value;
            }
            input.onkeyup = function(e) {
                if (e.keyCode === 13)
                    this.blur();
            }
        }
    }

    var tab = new Tab('#tab');
</script>

</html>